using System.Globalization;
using System.Text.RegularExpressions;
using System.Web;
using OFX.Aggregator.Infrastructure;

namespace OFX.Aggregator.Implementation.BESnet
{
	public abstract class StatementInterpreterBase : IStatementInterpreter
	{
		protected CultureInfo cultureInfo = CultureInfo.CurrentCulture;
		protected IRegexPatternProvider regexProvider;

		protected StatementInterpreterBase(string culture, IRegexPatternProvider regexProvider)
		{
			this.regexProvider = regexProvider;
			this.cultureInfo = CultureInfo.GetCultureInfo(culture);
		}

		#region IStatementInterpreter Members

		public abstract Statement InterpretStatement(string input);

		#endregion

		protected double RetrieveMatchValueAsCurrency(Match match, string captureName)
		{
			NumberFormatInfo numberFormatInfo = this.cultureInfo.NumberFormat;

			string str = RetrieveMatchValue(match, captureName).Trim();
			double amount = (str == "-" ? 0.0 : double.Parse(str, NumberStyles.Currency, numberFormatInfo));

			return amount;
		}

		protected static string RetrieveMatchValue(Match match, string captureName)
		{
			return HttpUtility.HtmlDecode(match.Groups[captureName].Value);
		}
	}
}